﻿package com.colorsstudio.defence.baseClass 
{
	import com.colorsstudio.defence.ConstantValues.Constants;
	import flash.display.Bitmap;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	/**
	 * ...
	 * @author wuzhida
	 */
	public class Slide extends Sprite 
	{
		public var _textField:TextField;
		public var _text:String;
		public var _bg:Sprite;
		public var _dragButton:SimpleButton;		
		public var DorUFlag:Boolean;
		public var _stage:Stage = Constants.main.stage;
		public var soundLine:Sprite;
		
		public function Slide(param_t:String) 
		{
			_text = param_t;			
			initB();
			initT();

			initS();
			this.addChild(_bg);
			this.addChild(soundLine);
			this.addChild(_textField);			
			this.addChild(_dragButton);
			draw();
			
			_stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown, false, 0, true);			
			_stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp, false, 0, true);	
					
		}
		
		private function initS():void 
		{
			soundLine = new Sprite();
			soundLine.x = _textField.width + Constants.POPUPWIN_OFFSET * 2
			soundLine.y =  _bg.height / 2 - Constants.POPUPWIN_OFFSET;
			
			_dragButton = new f_dragButton();
			_dragButton.x = _bg.width / 2;
			_dragButton.y = _bg.height / 2 - Constants.POPUPWIN_OFFSET;
			
		}
				
				
		private function mouseUp(e:MouseEvent):void 
		{
			trace("up");
			DorUFlag = false;
			_dragButton.removeEventListener(Event.ENTER_FRAME, move,false);	
		}
		
		private function move(e:Event):void 
		{
			var t:int = this.mouseX - _dragButton.width / 2;
			if (t > (_bg.width - Constants.POPUPWIN_OFFSET * 4))
			{
				_dragButton.x = _bg.width - Constants.POPUPWIN_OFFSET * 4
			}
			else if (t < (_textField.width + Constants.POPUPWIN_OFFSET * 2))
			{
				_dragButton.x =_textField.width + Constants.POPUPWIN_OFFSET * 2
			}
			else
			{
				_dragButton.x = t;
			}
			
			draw();
		}
		
		public function draw():void 
		{
			soundLine.graphics.clear();
			soundLine.graphics.lineStyle(4, 0x000000, 1);
			soundLine.graphics.moveTo(0, Constants.POPUPWIN_OFFSET);
			soundLine.graphics.lineTo(_bg.width - Constants.POPUPWIN_OFFSET * 4 - (_textField.width + Constants.POPUPWIN_OFFSET * 2), Constants.POPUPWIN_OFFSET);
			
			soundLine.graphics.lineStyle(4, 0x00ffcc, 1);
			soundLine.graphics.moveTo(0, Constants.POPUPWIN_OFFSET);
			soundLine.graphics.lineTo(_dragButton.x - (_textField.width + Constants.POPUPWIN_OFFSET * 2), Constants.POPUPWIN_OFFSET);
		}
		
		private function mouseDown(e:MouseEvent):void 
		{
			trace("down");
			if (e.target == _dragButton)
			{
				trace("target!");
				DorUFlag = true;
				_dragButton.addEventListener(Event.ENTER_FRAME, move,false,0,true);	
			}						
		}
		
		
		
		public function initB():void 
		{
			_bg = new f_settingBg();
		}
		
		public function initT():void 
		{
			var textFormat:TextFormat = new TextFormat();
			textFormat.font = "Maiandra GD";
			textFormat.size = 30;
			textFormat.color = 0xFFCC33;
			
			_textField = new TextField();
			_textField.defaultTextFormat = textFormat;
			//scoreT.height  = 100;
			//scoreT.width  = 250;
			_textField.autoSize = TextFieldAutoSize.LEFT;
			_textField.text = _text+" : ";	
			_textField.x = Constants.POPUPWIN_OFFSET;
			_textField.y = _bg.height / 2 - Constants.POPUPWIN_OFFSET;
		}
		
	}

}